package com.longxi.tianjintianbao.ui.presenter.houseinspection;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.api.ApiRetrofit;
import com.longxi.tianjintianbao.api.response.BaseResponse;
import com.longxi.tianjintianbao.exception.ExceptionHandle;
import com.longxi.tianjintianbao.exception.MySubscriber;
import com.longxi.tianjintianbao.model.houseinspection.HouseCompensate;
import com.longxi.tianjintianbao.ui.activity.houseinspection.AssetsDetailsActivity;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.ui.view.houseinspection.IAssetsCheckAtView;
import com.longxi.tianjintianbao.ui.view.houseinspection.IAssetsRepairAtView;
import com.longxi.tianjintianbao.utils.MyUtils;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;

import org.json.JSONException;
import org.json.JSONObject;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by cxw on & 2018/9/20
 */
public class AssetsRepairAtPresenter extends BasePresenter<IAssetsRepairAtView> {
    public String repairmode = "更换";
    public String recoverystate = "未恢复";
    private HouseCompensate houseCompensate;

    public AssetsRepairAtPresenter(BaseActivity context) {
        super(context);
    }

    public void initView() {
        mContext.setTitle("入住验房");
        mContext.setTvLeft("返回");
//        mContext.setTvRight("资产详情");
        houseCompensate = (HouseCompensate) mContext.getIntent().getSerializableExtra(AppConst.HOUSECOMPENSATE);

        getView().getTvAssetsLeft().setText("资产名称:" + houseCompensate.getName() +
                "\n资产编号:" + houseCompensate.getCode());
        getView().getTvAssetsRight().setText("资产类别:" + houseCompensate.getType());

        if(houseCompensate.getRepairmode().equals("更换")){
            getView().getRgRepairType().check(R.id.rbtnReplace);
            repairmode = "更换";
        }else if(houseCompensate.getRepairmode().equals("赔偿")){
            getView().getRgRepairType().check(R.id.rbtnCompensate);
            repairmode = "赔偿";
        }

        if(houseCompensate.getRecoverystate().equals("未恢复")){
            getView().getRgState().check(R.id.rbtnNoReturn);
            recoverystate = "未恢复";
        }else if(houseCompensate.getRecoverystate().equals("已恢复")){
            getView().getRgState().check(R.id.rbtnRecovered);
            recoverystate = "已恢复";
        }

        getView().getEtExplain().setText(houseCompensate.getExplain());

    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case PictureConfig.CHOOSE_REQUEST:
                // 图片选择结果回调
                addPicUtils.selectList = PictureSelector.obtainMultipleResult(data);
                gridImageAdapter.setList(addPicUtils.selectList);
                gridImageAdapter.notifyDataSetChanged();
                break;
        }
    }

    public void compensateSave(){
        String explain = getView().getEtExplain().getText().toString().trim();
        if(TextUtils.isEmpty(explain)){
            MyUtils.showToast("请输入情况描述");
            return;
        }

        mContext.showWaitingDialog(MyUtils.getString(R.string.load));
        JSONObject object = new JSONObject();
        try {
            object.put("userid", MyApplication.loginUser.userid);
            object.put("billid", houseCompensate.getBillid());
            object.put("explain", explain);
            object.put("repairmode", repairmode);
            object.put("recoverystate", recoverystate);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiRetrofit.getInstance().compensateSave(object)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new MySubscriber<BaseResponse>(mContext) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable throwable) {
                mContext.hideWaitingDialog();
                MyUtils.showToast(throwable.message);
            }

            @Override
            public void onNext(BaseResponse result) {
                mContext.hideWaitingDialog();
                if(result.getCode() != 0){
                    MyUtils.showToast(result.getMsg());
                    return;
                }

                mContext.setResult(mContext.RESULT_OK);
                mContext.finish();
            }
        });
    }
}
